package com.lanou3g.musicenjoy.activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.custom.DataCleanManager;

import java.util.HashSet;
import java.util.Set;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.jpush.android.api.JPushInterface;


/**
 * Created by zhangchunxin on 15/10/20.
 * 设置页面
 */
public class SettingActivity extends Activity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    @Bind(R.id.back)//返回
    ImageView back;
    @Bind(R.id.setting_clean_M)//清除缓存多少兆
    TextView settingCleanM;
    @Bind(R.id.setting_clean)//清除缓存
    RelativeLayout settingClean;
    @Bind(R.id.setting_switch)//开关
    CheckBox settingSwitch;
    @Bind(R.id.setting_send)//接收消息
    RelativeLayout settingSend;
    @Bind(R.id.setting_about)//关于
    RelativeLayout settingAbout;
    private SharedPreferences sharedPreferences;//记录开关状态
    private SharedPreferences.Editor editor;
    private String cache_size;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);

        back.setOnClickListener(this);
        settingClean.setOnClickListener(this);//缓存
        settingCleanM.setOnClickListener(this);//清除缓存
        settingSend.setOnClickListener(this);//推送
        settingSwitch.setOnCheckedChangeListener(this);//开关
        settingAbout.setOnClickListener(this);//关于

        sharedPreferences = getSharedPreferences("push", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();

        try {
            cache_size = DataCleanManager.getCacheSize(this.getCacheDir());
            cache_size = DataCleanManager.getCacheSize(getCacheDir());
            settingCleanM.setText(cache_size);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //记住开关状态

    @Override
    protected void onResume() {
        super.onResume();
        if (sharedPreferences.getBoolean("push", true))
            settingSwitch.setChecked(true);
        else
            settingSwitch.setChecked(false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                //页面切换时的动画效果
                overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
                break;
            case R.id.setting_clean://清除缓存
                DataCleanManager.deleteFolderFile(this.getCacheDir().toString(), true);
                try {
                    cache_size = DataCleanManager.getCacheSize(this.getCacheDir());
                    cache_size = DataCleanManager.getCacheSize(getCacheDir());
                    settingCleanM.setText(cache_size);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.setting_about:
                Intent intent = new Intent(this, AndyViewPagerActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked == false) {
            Set<Integer> days = new HashSet<Integer>();
            JPushInterface.setPushTime(getApplicationContext(), days, 0, 0);
            editor.putBoolean("push", false);
            editor.commit();
            Toast.makeText(SettingActivity.this, "关闭推送", Toast.LENGTH_LONG).show();
        } else {
            Set<Integer> days = new HashSet<Integer>();
            days.add(0);
            days.add(1);
            days.add(2);
            days.add(3);
            days.add(4);
            days.add(5);
            days.add(6);
            JPushInterface.setPushTime(getApplicationContext(), days, 6, 23);
            editor.putBoolean("push", true);
            editor.commit();
            Toast.makeText(SettingActivity.this, "接收推送", Toast.LENGTH_LONG).show();
        }

    }


}
